package com.example.magicapi.service;

import com.example.magicapi.mapper.CustomMapper;
import com.example.magicapi.util.RestResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author zk
 */
@Service
public class CustomService {
    @Resource
    private CustomMapper mapper;

    public List<Map<String, Object>> getCustomList(Map<String, Object> params) {
        params.put("del", "0");
        List<Map<String, Object>> customList = mapper.getCustomList(params);
        return customList;
    }

    public RestResult addCdStructuredCustom(Map<String, Object> params) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        params.put("customId", uuid);
        if (params.get("ext5") != null && !"".equals(params.get("ext5").toString())) {
            params.put("ext2", params.get("ext2") + "?menuShortName=" + params.get("ext5"));
        } else {
            params.put("ext2", params.get("ext2") + "?customId=" + params.get("customId"));
        }
        Integer i = mapper.add(params);
        if (i != 1) {
            return new RestResult(501, "插入报错", null);
        }
        return new RestResult(200, "添加成功", null);
    }

    public RestResult updateCdStructuredCustom(Map<String, Object> params) {
        Integer i = mapper.update(params);
        if (i != 1) {
            return new RestResult(501, "更新报错", null);
        }
        return new RestResult(200, "更新成功", null);
    }

    public RestResult judgeIfExistSameShortName(Map<String, Object> params) {
        if ("0".equals(params.get("customId"))) {
            params.remove("customId");
        }
        Integer count = mapper.judgeIfExistSameShortName(params);
        return new RestResult(200, "更新报错", "result", count);
    }

    public RestResult findByCondition(Map<String, Object> params) {
        params.put("del", "0");
        List<Map<String, Object>> list = mapper.findByCondition(params);
        return new RestResult(200, "查询成功", "detailList", list);
    }

    public RestResult getCdStructuredTemplate(Map<String, Object> params) {
        List<Map<String, Object>> list = mapper.getCdStructuredTemplate(params);
        return new RestResult(200, "查询成功", "cdStructuredTemplate", list);
    }

    @Transactional
    public RestResult addHtmlTemplate(Map<String, Object> params) {
        String templateId = UUID.randomUUID().toString().replaceAll("-", "");
        String detailId = UUID.randomUUID().toString().replaceAll("-", "");
        params.put("templateId", templateId);
        params.put("detailId", detailId);
        params.put("del", "0");
        params.put("shareRange", params.get("customId"));
        Integer i = mapper.addCdStructuredTemplate(params);
        if (i != 1) {
            return new RestResult(501, "插入报错", null);
        }
        if (!"999".equals(params.get("templateType")) && !"666".equals(params.get("templateType"))) {
            params.put("ext3", params.get("templateType"));
            params.put("ext2", params.get("templateContent"));
            params.put("ext1", params.get("templateName"));
            i = mapper.addCdStructuredCustomDetail(params);
            if (i != 1) {
                return new RestResult(501, "插入报错", null);
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("templateId", params.get("templateId"));
        result.put("detailId", params.get("detailId"));
        return new RestResult(200, "添加成功", result);
    }

    @Transactional
    public RestResult updateHtmlTemplate(Map<String, Object> params) {
        Integer i = mapper.updateCdStructuredTemplate(params);
        if (i != 1) {
            return new RestResult(501, "更新报错", null);
        }
        if (!"999".equals(params.get("templateType")) && !"666".equals(params.get("templateType"))) {
            params.put("ext3", params.get("templateType"));
            params.put("ext2", params.get("templateContent"));
            params.put("ext1", params.get("templateName"));
            i = mapper.updateCdStructuredCustomDetail(params);
            if (i != 1) {
                return new RestResult(501, "更新报错", null);
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("templateId", params.get("templateId"));
        result.put("detailId", params.get("detailId"));
        return new RestResult(200, "更新成功", result);
    }


    @Transactional
    public RestResult batchAddCdStructuredCustomDetail(Map<String, Object> params) {
        List<Map<String, Object>> detailList= (List<Map<String, Object>>) params.get("detailList");
        if(!CollectionUtils.isEmpty(detailList)){
            for (Map<String, Object> detail : detailList){
                String detailId = UUID.randomUUID().toString().replaceAll("-", "");
                detail.put("detailId", detailId);
            }
        }
        Map<String, Object> deleteDetail = new HashMap<>();
        deleteDetail.put("customId", params.get("customId"));
        deleteDetail.put("ext3", "666");
        mapper.deleteCdStructuredCustomDetailByParam(deleteDetail);
        if (detailList.size() > 0) {
            mapper.batchAddCdStructuredCustomDetail(detailList);
        }
        return new RestResult(200, "添加成功", "detailList", detailList);
    }


    public RestResult deleteCdStructuredCustomDetail(String customId) {
        Integer i = mapper.deleteCdStructuredCustomDetail(customId);
//        if (i != 1) {
//            return new RestResult(501, "删除报错", null);
//        }
        return new RestResult(200, "删除成功", null);
    }

    public void addCdStructuredCustomDetail(Map<String, Object> map) {
        mapper.addCdStructuredCustomDetail(map);
    }
}
